<template>
  <div class="overtime-setting">
    <form @submit.prevent="confirm" class="form">
      <div class="form-item middle-flex">
        <div class="label flex">
          <strong>记录类型</strong>
          <span></span>
        </div>
        <input v-model="data.type" placeholder="加班/请假">
      </div>
      <div @click="visible = true" class="form-item middle-flex">
        <div class="label flex">
          <strong>加班日期</strong>
          <span>(年-月-日)</span>
        </div>
        <div class="overtime-date">{{currentTime}}</div>
      </div>
      <div class="form-item middle-flex">
        <div class="label flex">
          <strong>加班时间</strong>
          <span>(小时)</span>
        </div>
        <input v-model="data.overtime" @change="changeOvertime" type="number" placeholder="加班时间">
      </div>
      <div class="form-item middle-flex">
        <div class="label flex">
          <strong>加班工资</strong>
          <span>(元)</span>
        </div>
        <input v-model="data.overtime_salary" type="number" placeholder="加班工资">
      </div>

      <div class="form-item middle-flex btn-box">
        <button class="btn" type="submit">确认</button>
      </div>
    </form>

    <calendar v-model="data.overtime_date" v-if="visible" :visible.sync="visible" />
  </div>
</template>

<script>
import moment from 'moment'

import Calendar from './Calendar.vue'
import setting from './setting.js'

export default {
  components: {
    Calendar
  },
  data() {
    const { overtimeSalary = 0 } = setting.get() // 默认单个小时工资

    return {
      overtimeSalary,
      visible: false,
      data: {
        overtime: '',
        type: '加班',
        overtime_salary: '',
        overtime_date: new Date()
      }
    }
  },
  computed: {
    currentTime() {
      const d = this.data.overtime_date
      const year = d.getFullYear()
      const month = d.getMonth() + 1
      const date = d.getDate()
      return `${year}年${month > 9 ? month : ('0' + month)}月${date}日`
    }
  },
  mounted() {
    this.fetchData()
  },
  methods: {
    async fetchData() {
      const { id } = this.$route.query
      if (!id) return
      const { overtime, type, overtime_salary, overtime_date } = await this.$http({
        url: `/api/overtime/record/${id}`
      })
      console.log({ overtime, type, overtime_salary, overtime_date })
      this.data = {
        overtime, type, overtime_salary,
        overtime_date: moment(overtime_date).toDate()
      }
    },
    changeOvertime() {
      if (!this.data.overtime_salary) {
        this.data.overtime_salary = this.data.overtime * this.overtimeSalary
      }
    },
    async confirm() {
      const data = { ...this.data }
      data.overtime_date = moment(data.overtime_date).format('YYYY-MM-DD')
      if (!data.type) {
        return this.$toast('需要输入记录类型')
      }
      if (!data.overtime) {
        return this.$toast('需要输入加班时间')
      }
      if (!data.overtime_salary) {
        return this.$toast('需要输入加班工资')
      }
      const { id } = this.$route.query
      if (!id) {
        await this.$http({
          url: '/api/overtime/record',
          method: 'post',
          data
        })
      } else {
        data.id = id
        await this.$http({
          url: '/api/overtime/record',
          method: 'put',
          data
        })
      }
      this.$router.push({ name: 'overtime' })
    }
  }
}
</script>

<style lang="scss" scoped>
.overtime-setting {
  min-height: 100vh;
  background: #f5f5f5;
}
.form {
  width: 100%;
  background: white;
}
.form-item {
  height: 1rem;
  border-bottom: 1px solid #eee;
  margin: 0 0.2rem;
  .label {
    white-space: nowrap;
  }
  input,
  .overtime-date {
    text-align: right;
    border: none;
    font-size: 0.46rem;
    flex: 1;
    &::placeholder {
      font-size: 0.32rem;
    }
  }
}
.btn-box {
  padding: 0.5rem 0;
}
.btn {
  width: 80%;
  margin: 0 auto;
  display: block;
  outline: 0 none;
  box-sizing: border-box;
  padding: 0;
  text-align: center;
  font-size: 18px;
  height: 38px;
  line-height: 38px;
  overflow: hidden;
  text-overflow: ellipsis;
  word-break: break-word;
  white-space: nowrap;
  color: #fff;
  background-color: #108ee9;
  border: 1px solid #108ee9;
  border-radius: 5px;
}
</style>
